package com.easylinkin.linkappapi.facerecognition.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class OssImageUrlToByteArrayUtil {


    public static byte[] imageUrlToByteArray(String imageUrl) {
        HttpClient client = new DefaultHttpClient();
        //3,设置请求方式
        HttpGet get = new HttpGet(imageUrl);
        //4,执行请求, 获取响应信息
        HttpResponse response = null;
        try {
            response = client.execute(get);
        } catch (IOException e) {
            return null;
        }
        if (response.getStatusLine().getStatusCode() == 200) {
            //得到实体
            HttpEntity entity = response.getEntity();
            byte[] data;
            try {
                data = EntityUtils.toByteArray(entity);
            } catch (IOException e) {
                return null;
            }
            return data;
        }

        return null;
    }


    public static void main(String[] args) {
        byte[] bytes = imageUrlToByteArray("http://linkthings-minio-in.dev.service.easylinkin.net:9000/test/linkapp/public/face/admin%4017371/20210415140300.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=liujiong%2F20210415%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210415T060301Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=e50288affb593cb120b719d3f7bc10e14ebbce6670aefcde7e22f937a459ea08");

        System.out.println(Base64Util.encode(bytes));
    }
}
